from django.forms.models import model_to_dict


# 模型转字典
def utils_model_to_dict(model, *args, **kwargs):
    data = model_to_dict(model)
    data["tid"] = model.tid
    # 处理日期时间字段，转换为字符串格式
    if hasattr(model, 'start_time') and model.start_time:
        data['start_time'] = model.start_time.strftime('%Y-%m-%d %H:%M:%S')
    if hasattr(model, 'end_time') and model.end_time:
        data['end_time'] = model.end_time.strftime('%Y-%m-%d %H:%M:%S')
    if hasattr(model, 'create_time'):
        data['create_time'] = model.create_time.strftime('%Y-%m-%d %H:%M:%S')
    if hasattr(model, 'update_time'):
        data['update_time'] = model.update_time.strftime('%Y-%m-%d %H:%M:%S')
    return data